//****************************************************************************
// Copyright (c) 2011-2013,WebJet Business Division,CYOU

// http://www.genesis-3d.com.cn

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the Software), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED AS IS, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//*****************************************************************************
Shader "FX_BasicWater" {
	Parameters{
	sampler2D [waveMap]<WaveMap> = "white"
	sampler2D [reflectiveColor]<ReflectiveColor> = "white"
	Float	[waveScale]<WaveScale> = "1.0"
	Vector  [waterColor]<WaterColor> = "0.286, 0.388, 0.541, 1.0"
	Vector	[WaterSpecColor]<WaterSpecColor> = "0.6, 0.6, 0.6, 1.0"
	float [SpecPower]<SpecPower> = "200"
	Vector [gWaveMapOffset0]<WaveMapOffset0> = "0.0,0.0,0.0,0.0"
	Vector [gWaveMapOffset1]<WaveMapOffset1> = "0.0,0.0,0.0,0.0"
	}
		RenderQueue "Opaque"
		
	

		CODEBEGIN

			sampler2D waveMap;
			sampler2D reflectiveColor;

			float4 gWaveMapOffset0;
			float4 gWaveMapOffset1;
			float4 waterColor;
			float waveScale;
			float4 WaterSpecColor;
			float SpecPower;
		
			struct INPUT
			{
				float2 _waveMap_UV;
				float2 _waveMap_UV2;
				float3 input_Normal;
				float3 input_CameraSpacePos;
				float3 input_ViewDir;
				float4 input_ClipSpacePos;
			};


			VS_OUTPUT mainVs( in float3 vPosition : POSITION,
							  	in float3 vNormal   : NORMAL,
							    in float2 vUv       : TEXCOORD0)
			{
				VS_OUTPUT Output;
				
				// transform the vertex into projection space
				float4 worldpos =  mul( g_Model ,float4( vPosition.x, vPosition.y, vPosition.z, 1.0f ) ); 
				float4 viewpos = mul( g_View ,worldpos ); 
				Output.Position = mul( g_Proj ,viewpos );
				
				float4 waveScale4 = float4(waveScale * 0.01,waveScale * 0.01,waveScale * 0.01 * 0.4, waveScale * 0.01 * 0.45);
				float4 temp;
	            temp.xy = vPosition.xz * waveScale4.xy;
				temp.zw = vPosition.xz * waveScale4.zw;
				Output.Uv = temp.xy + gWaveMapOffset0.xy;
				Output.ExternUvs.xy = temp.wz + gWaveMapOffset1.xy;


				Output.CameraSpacePos = viewpos;
				Output.Normal = vNormal;
				
				Output.ViewDir = normalize(g_CameraPos.xyz - worldpos.xyz);

				Output.ClipSpacePos = Output.Position;
				
				return Output;
			}
			
			float4 customlight(TEMPLATE_OUT sin, float3 viewDir, float lightAtten)
			{
				return sin.Diffuse;			
			}

			void mainTemplate(INPUT p, inout TEMPLATE_OUT o)
			{	
				float3 viewdir = normalize(p.input_ViewDir);
			
				
				float3 tanget   = float3(1.0f, 0.0f, 0.0f);
				float3 normal   = float3(0.0f, 1.0f, 0.0f);				
				float3 binormal = float3(0.0f, 0.0f, 1.0f);
			
			    float3x3 TBN = float3x3(tanget, binormal, normal);

				 float3 sampledNormalT1 = tex2D(waveMap, p._waveMap_UV).rgb;
			     sampledNormalT1        = 2.0 * sampledNormalT1 - 1.0;
			     sampledNormalT1        = normalize(sampledNormalT1);
			  
			     float3 sampledNormalT2 = tex2D(waveMap, p._waveMap_UV2).rgb;
			     sampledNormalT2        = 2.0 * sampledNormalT2 - 1.0;
			     sampledNormalT2        = normalize(sampledNormalT2);
			  
			     float3 sampledNormalT = normalize(0.5f*(sampledNormalT1 + sampledNormalT2));
				 float3 normalL = sampledNormalT.x * TBN[0] +
			 			          sampledNormalT.y * TBN[1] +
			 			          sampledNormalT.z * TBN[2];
			 	float3 normalW = mul(g_InvTransModel, float4(normalL, 0.0)).xyz;
				normalW        = normalize(normalW);

				float3 r = reflect(g_LightDir[0].xyz, normalW);
				r        = normalize(r);
				float  t  = pow(max(dot(r, viewdir), 0.0f), SpecPower);
				float s   = dot(g_LightDir[0].xyz, normalL);	
				if(s >= 0.0f)
				t = 0.0f;
				float4 spec   = t * WaterSpecColor;

				float fresnelFactor = dot(viewdir.xyz, normalL);
                float4 water = tex2D( reflectiveColor, float2(fresnelFactor,fresnelFactor));
				float3 lerpcolor3 =  lerp( water.rgb, waterColor.rgb, water.a );
	            float4 lerp_color;
				lerp_color.rgb = lerpcolor3.rgb;
	            lerp_color.a = waterColor.a;
	    	   
                o.Diffuse = lerp_color + spec;
				o.Alpha = lerp_color.a;

				
			}
			CODEEND
			Technique "FX_BasicWater"{
		     Pass{
				Compiler
				{
					VertexShader = mainVs
					Template = mainTemplate			
					#PARAMETER  customlight			
				}
		
			   RenderSTATE { 
							    cullMode "Back"
							    ZTest On
							    ZWrite On
							    blendMode [Add] "SRCALPHA" "INVSRCALPHA"
							    alphatest Off
							    samplerState "waveMap" [Wrap] [Linear]
							    samplerState "reflectiveColor" [Wrap][LINEAR]
						    }
		           }
	         }
}
